<?php
/**
 * @Author:      zwh
 * @DateTime:    2016-11-16 15:58:29
 * @Description: 返现比率
 */
class KickbackRate extends Eloquent
{
    protected $table = "kickback_rate";
    public static $type = [1=>"交强", 2=>"车船税", 3=>"商业险"];
    
    public static function mathAmount($amount,$math) {
        $math = str_replace("money", $amount, $math);
        return eval("return $math;");
    }
    
    /**
     * 根据金额和规则返回 计算公式，返现金额，返现类型（现金or充值）
     * @param type $amount
     * @param type $kr_id
     */
    public static function amountInfo($amount,$kr_id){
        $kr=KickbackRate::select("kickback_rate.is_rechange","kickback_config.math")
                                ->leftJoin("kickback_config","kickback_rate.con_id","=","kickback_config.id")
                                ->where("kickback_rate.id",$kr_id)
                                ->where("kickback_rate.status",1)
                                ->first();
        if (empty($kr)) return false; 
        $amount = KickbackRate::mathAmount($amount,$kr->math);//返现金额
        return ["amount"=> $amount,"is_rechange"=>$kr->is_rechange];
        
    }
    
    public static function rateInfo($kickback_rate_id,$type){
        $info = KickbackRate::select("kickback_rate.*","kickback_config.math")
                ->leftJoin("kickback_config","kickback_config.id","=","kickback_rate.con_id")
                ->where("kickback_rate.id",$kickback_rate_id)
                ->where("kickback_rate.type",$type)
                ->first();
        return $info;
    }
}